package ckw.database.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import core.database.AbstractEntity;

@Entity
@Table(name = "electionNonResidentVoter")
public class ElectionNonResidentVoter extends AbstractEntity {
	@Column(name = "hasVoted", nullable = false)
	private Boolean hasVoted = false;

	@ManyToOne(cascade = { CascadeType.DETACH })
	@JoinColumn(name = "voter", insertable = true, updatable = true)
	private Voter voter;

	@ManyToOne
	@JoinColumn(name = "election", insertable = true, updatable = true)
	private Election election;

	public ElectionNonResidentVoter() {
	}

	public ElectionNonResidentVoter(Voter voter, Election election) {
		super();
		this.voter = voter;
		voter.getNonResidentElections().add(this);
		this.election = election;
		election.getVoters().add(this);
	}

	public Boolean getHasVoted() {
		return hasVoted;
	}

	public void setHasVoted(Boolean hasVoted) {
		this.hasVoted = hasVoted;
	}

	public Voter getVoter() {
		return voter;
	}

	public void setVoter(Voter voter) {
		this.voter = voter;
	}

	public Election getElection() {
		return election;
	}

	public void setElection(Election election) {
		this.election = election;
	}

	public void removeFromElections() {
		voter.getNonResidentElections().remove(this);
		voter = null;
		election.getVoters().remove(this);
		election = null;
	}

	public void addToElections(Voter voter, Election election) {
		this.voter = voter;
		voter.getNonResidentElections().add(this);
		this.election = election;
		election.getVoters().add(this);
	}
}
